<template>
  <!-- 菜单 -->
  <div>
    <div>
      <table>
        <thead>
          <tr>
            <th>尺寸</th>
            <th>价格</th>
            <th>加入</th>
          </tr>
        </thead>
        <tbody class="table" v-for="(item,index) in getMenuItems" :key="index">
          <tr v-for="(options,index) in item.options " :key="index">
            <td class="td">{{options.size1 }}</td>
            <td class="td">{{options.price1 }}</td>
            <td class="td">
              <button class="btn" @click="addToBasket(item,options)">+</button>
            </td>
          </tr>
        </tbody>
      </table>
    </div>

    <!-- shopping -->
    <div class="shopping">
      <!-- <div>购物车没有东西~~</div> -->
      <table>
        <thead>
          <tr>
            <th class="th1">数量</th>
            <th class="th1">品种</th>
            <th class="th1">价格</th>
          </tr>
        </thead>
        <tbody v-for="(item,index) in baskets" :key="index">
          <tr v-if="baskets.length > 0">
            <td class="td1">
              <span @click="sub(item)">-</span>
              <span class="span">{{item.quantity}}</span>
              <span @click="add(item)">+</span>
            </td>
            <td class="td1">{{item.name}}</td>
            <td class="td1">{{item.price}}</td>
          </tr>
          <div v-else>购物车没有东西</div>
        </tbody>
      </table>
      <div>总价：{{total}}RMB</div>
      <el-button type="success">提交</el-button>
    </div>
  </div>
</template>
<script>
import axios from "axios";
export default {
  data() {
    return {
      baskets: [],
      getMenuItems: []
    };
  },
  created() {
    this.fetchData();
  },
  computed: {
    total() {
      let totalCost = 0;
      for (let index in this.baskets) {
        let indivdualItem = this.baskets[index];
        totalCost += indivdualItem.quantity * indivdualItem.price;
      }
      return totalCost;
    }
  },
  methods: {
    sub(item) {
      item.quantity--;
      if (item.quantity <= 0) {
        this.removeBasket(item);
      }
    },
    removeBasket(item) {
      this.baskets.splice(this.baskets.indexOf(item), 1);
    },
    add(item) {
      item.quantity++;
    },
    fetchData() {
      axios.get("menu.json").then(res => {
        console.log(res.data);
        this.getMenuItems = res.data;
      });
    },
    addToBasket(item, options) {
      // console.log(item);
      // console.log(options);
      let basket = {
        name: item.name,
        size: options.size1,
        price: options.price1,
        quantity: 1
      };
      if (this.baskets.length > 0) {
        let result = this.baskets.filter(basket => {
          return basket.name === item.name && basket.price === options.price;
        });
        if (result != null && result.length > 0) {
          result[0].quantity++;
        } else {
          this.baskets.push(basket);
        }
      } else {
        this.baskets.push(basket);
      }
    }
  }
};
</script>
<style lang="less" scope>
// .td {
//   padding: 30px 100px;
//   border: 1px solid #ccc;
// }
.btn {
  background-color: yellow;
  color: rgb(127, 145, 24);
  font-size: 20px;
  border: 1px solid aqua;
}
.table {
  float: left;
}
.shopping {
  float: right;
  margin: 40px 30px 0 0;
  .el-button {
    width: 200px;
  }
}
.span {
  padding: 0 10px;
}
</style>